package com.sxt.core.provider;

import java.util.List;

import com.sxt.core.context.IocContext;
import com.sxt.core.model.BeanDefine;
import com.sxt.core.provider.listener.ListenerServer;
import com.sxt.core.provider.method.IocObjectInvoker;
import com.sxt.core.provider.scan.impl.IOCPackageScan;
import com.sxt.core.utils.FactoryBeanUtil;
import com.sxt.core.utils.PackageScanUtil;
import com.sxt.core.utils.PropInjectUtil;

public class StartProvider {

	public static void start(int port,String packages) {
		// ioc 的加载过程
		IocContext iocContext = new IocContext();
		//	扫描普通的包
		List<BeanDefine> beanDefines = PackageScanUtil.packagSanAndGetBeanDefine(packages);
		// 服务的暴露实现
		IOCPackageScan exposerService  = new  IOCPackageScan();
		
		List<BeanDefine> beanDefineExposeServices = exposerService.scanPackage(packages);
		beanDefines.addAll(beanDefineExposeServices);
		// 创建对象
		FactoryBeanUtil.createObject(beanDefines, iocContext);
		
		// 注入属性
		PropInjectUtil.injectProps(beanDefines, iocContext);
		Regeister bean = (Regeister)iocContext.getBean("zkRegeisterImpl");//TreeMap 的问题 
		System.out.println(bean);
		exposerService.setRegiester(bean);
		exposerService.complateRegeister(port);
		//设置ioc
		IocObjectInvoker invokeLocalMethod = (IocObjectInvoker)iocContext.getBean(InvokeLocalMethod.class);
		invokeLocalMethod.setIocContext(iocContext);
		
		ListenerServer listenerServer = (ListenerServer)iocContext.getBean("listenerServer");
		
		listenerServer.listener(port);
	}

	public static void main(String[] args) {
		start(8888, "com.sxt.core.provider.zk,com.sxt.core.net.impl,com.sxt.core.codc.impl,com.sxt.core.provider.method");
		//IocObjectInvoker 无法注册ioc 该容器，想想怎么注入进去
		//服务如何上线-> 怎么注册（想想需要在那里调用）
	}
}
